package com.qinglei.recoup;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamSort {
    public static void main(String[] args) {
        List<Person> javaProgrammers = new ArrayList<Person>() {
            {
                add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 43, 2000));
                add(new Person("Tamsen", "Brittany", "Java programmer", "female", 23, 1500));
                add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1300));
//                add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600));
//                add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200));
//                add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900));
//                add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300));
//                add(new Person("Jayden", "Corrina", "Java programmer", "female", 35, 1700));
//                add(new Person("Palmer", "Dene", "Java programmer", "male", 33, 2000));
//                add(new Person("Addison", "Pam", "Java programmer", "female", 34, null));
            }
        };
        System.out.println("根据 name 排序,并显示前5个 Java programmers:");
        List<Person> sortedJavaProgrammers = javaProgrammers
                .stream()
                .sorted((p, p2) -> (p.getFirstName().compareTo(p2.getFirstName())))
                .limit(5)
                .collect(Collectors.toList());

        sortedJavaProgrammers.forEach((p) -> System.out.printf("%s %s; %n", p.getFirstName(), p.getLastName()));

        System.out.println("根据 salary 排序 Java programmers:");
        sortedJavaProgrammers = javaProgrammers
                .stream()
               // .sorted( (p, p2) -> (p.getSalary() - p2.getSalary()))
               // .sorted( (p, p2) -> p.getSalary().compareTo(p2.getSalary()))
                .sorted(Comparator.comparing(Person::getSalary).reversed())
                //.sorted(Comparator.comparing(Person::getSalary))
                .collect(Collectors.toList());

        sortedJavaProgrammers.forEach((p) -> System.out.printf("%s %s; %n", p.getSalary(), p.getLastName()));

        double person = javaProgrammers
                .stream()
               // .max((p, p2) -> (p.getSalary() - p2.getSalary()))
                .min((p, p2) ->p.getSalary().compareTo(p2.getSalary())).map(Person::getSalary).get();
               // .mapToDouble(p1->p1.getSalary())
               // .min().getAsDouble();
              // .min((p, p2) ->p.getSalary().compareTo(p2.getSalary())).get().getSalary();

        //System.out.printf("Name: %s %s; Salary: $%,d.", person);

        System.out.println("\"1Name:"+person+"++++++++++++++++++++++++++");

        double person1 = javaProgrammers
                .stream()
                // .max((p, p2) -> (p.getSalary() - p2.getSalary()))
                //.min((p, p2) ->p.getSalary().compareTo(p2.getSalary()))
                //.mapToDouble(Person::getSalary)
                .mapToDouble(Person::getSalary)
                .average().getAsDouble();
        // .get();
        //System.out.printf("Name: %s %s; Salary: $%,d.", person);

        System.out.println("\"Nameavg:"+person1+"++++++++++++++++++++++++++");
    }


}
